Previous | Home | Next |
Tag description: JSF <f:validateLength> tag registers a LengthValidator instance on the UIcomponent associated with the enclosing tag. LengthValidator is a Validator that checks the number of characters in the String representation of the value of the associated component.
Code
<f:validateLength>
Example:
Step 1: Welcome page of Example
<%-- Name= welcomeJSF.jsp --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%-- This file is an entry point for JavaServer Faces application. --%> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>r4r.co.in</title> </head> <body> <h1><h:outputText value="Tag <f:validateLength> Example"/></h1> <h:form id="validateLength"> <%-- Display Error Message --%> <h:message for="validateLength:password" style="color:red" /> <h:panelGrid columns="2" cellpadding="2" cellspacing="2"> <h:outputLabel value="Choose password" /> <h:inputSecret id="password" value="#{validateLength.password}" required="true"> <%-- Used <f:validateLength> Tag --%> <f:validateLength minimum="10" maximum="15" /> <%-- Used <f:validateRegex> Tag --%> <f:validateRegex pattern="(^[0-9a-zA-Z]*$)" /> </h:inputSecret> <h:commandButton action="#{validateLength.submit()}" value="Submit" /> <h:commandButton action="#{validateLength.reset()}" value="Reset" /> </h:panelGrid> <BR><BR> <%-- Display Result --%> <h:panelGrid rendered="#{validateLength.flag != false}" > Password: <h:outputText value="#{validateLength.password}" /> </h:panelGrid> </h:form> </body> </html> </f:view>
Step 2: ManagedBean class for provide logic in program
/* * save as a validateLengthBean.java */ package r4r.JSF2; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; /* -- name of bean -- */ @ManagedBean(name = "validateLength") @RequestScoped public class validateLengthBean { private String password; //Create a flag with initial value false private boolean flag = false; /* -- Getter/Setter -- */ public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /* -- submit method -- */ public String submit() { flag = true; return "submit"; } /* -- reset method -- */ public String reset() { password = null; flag = false; return "reset"; } }





Previous | Home | Next |